package coculater;

public class cul {
    public static void main(String[] args) {
       ArrayStack arrayStack=new ArrayStack(10);
       ArrayStack arrayStack1=new ArrayStack(10);
       String ex="70+2*6-2";
       int index=0;
       int nums1=0;
       int nums2=0;
       char ch=' ';
       String s="";
       int oper=0;
       int res=0;
       while (true){
           ch=ex.substring(index,index+1).charAt(0);
           if (arrayStack1.isoper(ch)){//判断是否为运算符
               if (!arrayStack1.isnull()){//判断是否为空
                   if (arrayStack1.priority(ch)<=arrayStack1.priority(arrayStack1.peek())){
                       nums1=arrayStack.pop();
                       nums2=arrayStack.pop();
                       oper=arrayStack1.pop();
                       res= arrayStack.cal(nums1,nums2,oper);
                       arrayStack.pussh(res);
                       arrayStack1.pussh(ch);
                   }else {
                       arrayStack1.pussh(ch);
                   }
               }else {
                   arrayStack1.pussh(ch);
               }
           }else {
               s+=ch;
               if (index==ex.length()-1){
                   arrayStack.pussh(Integer.parseInt(s));
               }else if (arrayStack1.isoper(ex.substring(index + 1, index + 2).charAt(0))){
                       arrayStack.pussh(Integer.parseInt(s));
                       s="";
               }
           }
           index++;
           if (index>=ex.length()){
               break;
           }
       }

       while (true){
           if (arrayStack1.isnull()){
               break;
           }
           nums1= arrayStack.pop();
           nums2= arrayStack.pop();;
           oper= arrayStack1.pop();
           res= arrayStack.cal(nums1,nums2,oper);
           arrayStack.pussh(res);//入栈
       }
        System.out.println("结果"+res);
    }
}
class ArrayStack{
    private int maxSize;
    private int[] stack;
    private int top=-1;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack=new int[this.maxSize];
    }

    public boolean isfull(){//满了
      return top==maxSize-1;
    }
    public boolean isnull(){
        return top==-1;
    }
    public void pussh(int values){
        if (isfull()){
            System.out.println("数组满了");
            return;
        }
        top++;
        stack[top]=values;
    }

    public int pop(){
        if (isnull()){
            throw new RuntimeException("数组为空");
        }
        int values=stack[top];
        top--;
        return values;
    }
    public void show(){
        if (isnull()){
            throw new RuntimeException("数组为空");
        }
        for (int i = top; i>=0; i--) {
            System.out.println(stack[i]);
        }
    }
    public int priority(int oper){
        if (oper=='*'||oper=='/'){
            return 1;
        } else if (oper=='+'||oper=='-') {
            return 0;
        }else {
            return -1;
        }
    }
    public boolean isoper(char val){
        return val=='*'||val=='/'||val=='+'||val=='-';
    }
    public int cal(int num1,int num2,int oper){
        int res=0;
        switch (oper){
            case '+':
                res=num1+num2;
                break;
            case '-':
                res=num2-num1;
                break;
            case '*':
                res=num1*num2;
                break;
            case '/':
                res=num2/num1;
                break;
            default:break;
        }
        return res;
    }
    public int peek(){
        return stack[top];
    }
}